var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    images_arr: [],
    name: '',
    money: '',
    buthidden: false,
    visible1: false
  },

  handleOpen1() {
    this.setData({
      visible1: true
    });
  },

  handleClose1() {
    this.setData({
      visible1: false
    });
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},
  //上传图片
  upload: function(item) {
    var that = this;
    wx.showLoading({
      title: '上传中'
    });
    wx.uploadFile({
      url: app.preUrl + 'uploadFile',
      header: {
        'Content-Type': 'multipart/form-data'
      },
      filePath: item,
      name: 'file',
      formData: {
        userId: app.globalData.userId //附加信息为用户ID
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        var imgUrl =
          'https://www.lvdaxia.com.cn/dabaozhan/fileImage/' +
          JSON.parse(res.data).result;
        that.setData({
          images_arr: that.data.images_arr.concat(imgUrl)
        });
      }
    });
  },
  upimg: function() {
    let that = this;
    let len = 0;
    len = 4 - this.data.images_arr.length;
    if (len == 0) {
      wx.showToast({
        title: '最多上传4张图片',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.chooseImage({
      count: len,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(res);
        tempFilePaths.forEach(function(item, index) {
          that.upload(item);
        });
      }
    });
  },
  // 删除图片
  deleteImg: function(e) {
    var that = this;
    var images = that.data.images_arr;
    var index = e.currentTarget.dataset.index;
    images.splice(index, 1);
    that.setData({
      images_arr: images
    });
  },

  inputname: function(e) {
    var that = this;
    that.setData({
      name: e.detail.value
    });
  },

  inputmoney(e) {
    var that = this;
    that.setData({
      money: e.detail.value
    });
  },
  bindFormSubmit: function(e) {
    var that = this;
    if (that.data.name == '') {
      wx.showToast({
        title: '付款人姓名必选',
        icon: 'none'
      });
      return false;
    } else if (that.data.money == '') {
      wx.showToast({
        title: '转入金额必填',
        icon: 'none'
      });
      return false;
    } else if (that.data.images_arr.length == 0) {
      wx.showToast({
        title: '图片必填',
        icon: 'none'
      });
      return false;
    } else {
      console.log(that.data.images_arr);
      wx.showLoading({ title: '提交中' });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'saveUserRecharge',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId,
          name: that.data.name,
          money: that.data.money,
          imgList: that.data.images_arr
        },
        success: function(res) {
          wx.hideLoading();
          console.log(res);
          if (res.data.code == 100) {
            that.handleOpen1();
          } else {
            wx.showModal({
              content: res.data.message,
              confirmColor: '#50BEC5',
              confirmText: '知道了',
              showCancel: false,
              success(res) {}
            });
          }
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
